% Normal subject
num_choose = {'01'};
task_choose = {'FR','LR'};
component_choose = [2,3,4,5];

% fixed parameter
fs=1000;
mid=300;

% choose data path & save path
[subject_folder,synergy_sim_save,sep_typle] = file_path();

for si = 1:1:length(num_choose)
    subject_num = num_choose(si);
        
        for task_i = 1:1:length(task_choose)
            task = task_choose(task_i);
            subject_data = strcat(subject_folder,'c',subject_num,sep_typle,task,sep_typle);
            
            for syn_i = 1:1:length(component_choose)
                syn_component = component_choose(syn_i);
               %% calculation  EMG         
                Files = dir(subject_data{1,1});
                LengthFiles = length(Files);
                EMGMatrix = [];
                % preprocess EMG
                for j = 3:LengthFiles
                    % load raw data
                   [raw_data] = load_raw_data(subject_data,Files,j);% load raw data
                    % calculate onset & offset
                    [movement] = calculate_onset_offset( raw_data,fs,mid );

                    % EMG preprocess
                    [ EMG_p ] = EMG_preprocess( raw_data,movement); 
                    EMGMatrix = cat(1, EMGMatrix, EMG_p(1:1000,1:7));

                    [ kinematics(j-2) ] = calculate_kinematics_data(raw_data,fs,mid,movement); % Kinematics
                end
                %  decompose synergy(NNMF)
                for k = 1:1:7
                    [synergy_subject(k)] = NNMF_Sync(abs(EMGMatrix),k); % NNMF 
                end

%                 % calculate synergy similarity
%                 [sim] = calculate_synergy_sim( synergy_subject,task,syn_component );
%                 sim_struct{syn_i,1} = sim;
            end
            save_syn_path = strcat(synergy_sim_save,'synergy_c',subject_num,'_',lower(task),'.mat');
            save(save_syn_path{1,1},'synergy_subject');
            
%               % save similarity.mat
%              save_path = strcat(synergy_sim_save,'synergy_sim_c',subject_num,'_',lower(task),'.mat');
%              save(save_path{1,1},'sim_struct');
             
             save_path = strcat(synergy_sim_save,'kinematics_c',subject_num,'_',lower(task),'.mat');
             save(save_path{1,1},'kinematics');    
             
             clear kinematics
        end
       
end
